<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>高阶函数AOP</title>
  </head>
  <body>
    <script>
      Function.prototype.before = function (beforeFn) {
        return () => {
          beforeFn.apply(this, arguments); // 执行before函数
          return this.apply(this, arguments); // 执行本函数
        };
      };
      Function.prototype.after = function (afterFn) {
        return () => {
          var ret = this.apply(this, arguments);
          afterFn.apply(this, arguments); // 执行before函数
          return ret; // 执行本函数
        };
      };
      func = function () {
        console.log(2);
      }
        .before(function () {
          console.log(1);
        })
        .after(function () {
          console.log(3);
        });
      func();
    </script>
  </body>
</html>
